﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using csharp.rad.general;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.GOVhotel
{
    public partial class CompaRet : KryptonForm
    {
        Database myDBase;
        public static String Ação;
        ToolStripLabel[] titCame = new ToolStripLabel[25];
        int ttC, qCol = 0, difData, qRow, qtItens, nmPag;
        DateTime datada;

        public CompaRet(Database myDBase)
        {
            InitializeComponent();
            this.myDBase = myDBase;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            Cameiras rolCame = new Cameiras(myDBase);
            int dbRet = rolCame.Lista();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                    + "referência: CompaRetLoadCames" + "\n" + rolCame.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Font myFont = new Font("MicroSoft Sans Serif", 14);
            for (ttC = 0; ttC < rolCame.Items.Count; ttC++)
            {
                titCame[ttC] = new ToolStripLabel();
                titCame[ttC].AutoSize = true;
                titCame[ttC].Width = 40;
                titCame[ttC].Image = titProds.Image;
                titCame[ttC].Font = myFont;
                titCame[ttC].Text = rolCame.Items[ttC].Nome;
                titCame[ttC].TextImageRelation = TextImageRelation.ImageBeforeText;
                titCame[ttC].Tag = rolCame.Items[ttC].Código;
                titCame[ttC].TextAlign = ContentAlignment.BottomLeft;
                titCame[ttC].Owner = toolCames;
                toolCames.Items.Add(titCame[ttC]);
                DataGridViewColumn coluna = (DataGridViewColumn)Coluna_X.Clone();
                coluna.Name = "Coluna_" + ttC.ToString();
                listaCompa.Columns.Add(coluna);
            }
            listaCompa.Columns[ttC + 1].Visible = false;
            datada = Convert.ToDateTime("01/" + DateTime.Today.ToString("MM/yyyy"));
            datada_Changed();
        }

        private void cmdVoltar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void datada_Changed()
        {
            menuMêsAno.Text = datada.ToString("MMMM / yyyy");
            listaCompa.Rows.Clear();
            Produtos rolProd = new Produtos(myDBase);
            int dbRet = rolProd.Lista();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                    + "referência: CompaRetListaProd" + "\n" + rolProd.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int prd = 0; prd < rolProd.Items.Count; prd++)
            {
                listaCompa.Rows.Add(rolProd.Items[prd].Descrição.Trim());
                int qRow = listaCompa.Rows.Count - 1;
                for (int pox = 0; pox <= ttC; pox++)
                { listaCompa.Rows[qRow].Cells[pox + 1].Value = "0"; }
                Retiradas rolSaída = new Retiradas(myDBase);
                rolSaída.Produto = rolProd.Items[prd].Código;
                rolSaída.DataIni = Convert.ToDateTime(datada.ToString("01/MM/yyyy"));
                rolSaída.DataFim = rolSaída.DataIni.AddMonths(1);
                dbRet = rolSaída.ListaMêsCame();
                if (dbRet != 0)
                {
                    MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                        + "referência: CompaRetListaRet" + "\n" + rolSaída.MsgErro,
                        "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                for (int cm = 0; cm < rolSaída.Items.Count; cm++)
                {
                    for (int colX = 0; colX <= ttC; colX++)
                    {
                        if (Convert.ToInt32(titCame[colX].Tag) == rolSaída.Items[cm].Cameira)
                        {
                            listaCompa.Rows[qRow].Cells[colX + 1].Value = rolSaída.Items[cm].Quantidade.ToString();
                            break;
                        }
                    }
                }
            }
            listaCompa.Sort(Column0, ListSortDirection.Ascending);
        }

        private void cmdLeft_Click(object sender, EventArgs e)
        {
            for (int ix = 0; ix < 3; ix++)
            {
                titCame[qCol].Visible = false;
                listaCompa.Columns[qCol+1].Visible = false;
                if (qCol < (ttC-15))
                {
                    qCol = qCol + 1;
                }
                else
                {
                    cmdLeft.Enabled = false;
                    break;
                }
                cmdRight.Enabled = true;
            }
        }

        private void cmdRight_Click(object sender, EventArgs e)
        {
            for (int ix = 0; ix < 3; ix++)
            {
                titCame[qCol].Visible = true;
                listaCompa.Columns[qCol + 1].Visible = true;
                if (qCol > 0)
                {
                    qCol = qCol - 1;
                }
                else
                {
                    cmdRight.Enabled = false;
                    break;
                }
                cmdLeft.Enabled = true;
            }
        }

        private void menuPrint_Click(object sender, EventArgs e)
        {
            qtItens = listaCompa.Rows.Count;
            qRow = 0;
            PrintDocument docto = new PrintDocument();
            docto.DefaultPageSettings.Landscape = true;
            docto.PrintPage += new PrintPageEventHandler(this.Docto_PrintPage);
        }

        private void Docto_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Pen myPen = new Pen(Brushes.Black);
            ev.Graphics.DrawLine(myPen, 30, 53, 1080, 53);
            Font myFont1 = new Font("Arial", 14);
            Font myFont2 = new Font("Lucida Console", 10);
            Font myFont3 = new Font("Lucida Sans", 10, FontStyle.Underline);
            Font myFont4 = new Font("Lucida Sans", 10);
            StringFormat strFormat = new StringFormat();
            strFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            string loja = "Parque das Águas Quentes";
            ev.Graphics.DrawString(loja, myFont1, Brushes.Black, 30, 30);
            ev.Graphics.DrawString("Comparativo de Retiradas por Camareira", myFont1, Brushes.Black, 400, 30);
            ev.Graphics.DrawString(datada.ToString("MMMM"), myFont1, Brushes.Black, 940, 30);
            ev.Graphics.DrawString(datada.ToString("/yyyy"), myFont1, Brushes.Black, 1028, 30);
            if (qRow > 0)
            {
                nmPag = nmPag + 1;
                ev.Graphics.DrawString("pág. " + nmPag.ToString(), myFont1, Brushes.Black, 840, 30);
            }
            ev.Graphics.DrawString("Produtos", myFont3, Brushes.Black, 30, 177);
            int pox = 160;
            int posXX = pox + 32;
            for (int qq = 0; qq < ttC; qq++)
            {
                string edtNome = titCame[qq].Text.Trim();
                edtNome = edtNome.PadLeft(15);
                ev.Graphics.DrawString(edtNome, myFont2, Brushes.Black, posXX, 60, strFormat);
                posXX = posXX + 40;
            }
            int yPos = 0;
            for (int nmLin = 0; nmLin < 28; nmLin++)
            {
                if (qRow >= qtItens) break;
                yPos = (nmLin * 20) + 207;
                ev.Graphics.DrawString(listaCompa.Rows[qRow].Cells[0].Value.ToString(),
                    myFont4, Brushes.Black, 30, yPos);
                int poxXX = pox;
                for (int qCol = 1; qCol < (ttC + 1); qCol++)
                {
                    string edita = listaCompa.Rows[qRow].Cells[qCol].Value.ToString();
                    edita = edita.PadLeft(5);
                    ev.Graphics.DrawString(edita, myFont2, Brushes.Black, poxXX, yPos);
                    poxXX = poxXX + 40;
                }
                qRow = qRow + 1;
            }
            if (qRow >= qtItens) ev.HasMorePages = false;
            else
            {
                ev.Graphics.DrawString("- - - - - - - - - - - - - - - - - - - -", myFont1, Brushes.Black, 200, yPos + 14);
                ev.HasMorePages = true;
            }
        }

        private void menuMêsMenos_Click(object sender, EventArgs e)
        {
            datada = datada.AddMonths(-1);
            difData = difData - 1;
            if (difData < -2) menuMêsMenos.Enabled = false;
            menuMêsMais.Enabled = true;
            datada_Changed();
        }

        private void menuMêsMais_Click(object sender, EventArgs e)
        {
            datada = datada.AddMonths(1);
            difData = difData + 1;
            if (difData == 0) menuMêsMais.Enabled = false;
            menuMêsMenos.Enabled = true;
            datada_Changed();
        }
    }
}
